knitr::opts_chunk$set(echo = TRUE, warning = FALSE, eval = TRUE)
knitr::opts_knit$set(root.dir = normalizePath("../"))
#always looking from the perspective of where the rmd is, "../" says look outside of this folder
#~ is a shortcut for your home directory/root folder
#install.packages("OpenStreetMap")
library (OpenStreetMap)
## Warning: package 'OpenStreetMap' was built under R version 3.3.3
library (rgdal)
## Loading required package: sp
## rgdal: version: 1.2-5, (SVN revision 648)
## Geospatial Data Abstraction Library extensions to R successfully loaded
## Loaded GDAL runtime: GDAL 2.0.1, released 2015/09/15
## Path to GDAL shared files: C:/Users/ebola/Documents/R/win-library/3.3/rgdal/gdal
## Loaded PROJ.4 runtime: Rel. 4.9.2, 08 September 2015, [PJ_VERSION: 492]
## Path to PROJ.4 shared files: C:/Users/ebola/Documents/R/win-library/3.3/rgdal/proj
## Linking to sp version: 1.2-4
library(sp)
library(maptools)
## Checking rgeos availability: TRUE
library (raster)
In this lab you will begin to get used to using R with spatial data and practice your map making in R for a variety of data types.
Start by reading through sections 7.1-7.3 in Arnold and Tilton.2015. Humanities Data in R.
Scatterplots: -scatter plot: thing of x= longitude, y= latitude To make scatter plot with underlying map: 1. plot your points 2. snippets package (maybe can also use OpenStreetMap package) pulls in an underlying map of ~25 tiles using osmap 3. then use points function to put the points back ontop of the map tiles 4. to set size of map, in plot, asp parameter sets the aspect ratio,the ratio of the scale on the y-axis to the scale of the x-axis, of a plot. If this is set to the ratio of the length of a degree of latitude to the length of a degree of longitude, the resulting map will be undistorted. -maps from Stamen Design with osmap function are black & white and less busy than OpenStreetMaps
ShapeFiles: -Map tiles don’t actually have information about the roads or geo features, they are just pixels (raster data.) -Vector data has meta-data attached to objects, and shapefiles are a way to store vectorize geospatial data -the SpatialPolygonsDataFrame has both geospatial information and a dataframe with metadata -plot these with projections, use rgdal package if needed to convert projections
Then read through ‘Spatial Data Manipulation’ on RSpatial. [http://rspatial.org/spatial/index.html]. There’s a lot here, so feel free to move through it quickly and think of this as reference.
#a) Provided Map
datapath <- "../GEO200CN/data"
UCplaces <- read.csv(file.path(datapath, "UCplaces.csv"))
map <- openmap (c(38.55, -121.77), c(38.53, -121.74)) #openmap function gets a map using upper-left and lower-right corners (function knows that order)
plot (map)
mapLatLong <- openproj(map) #openproj converts from the default mercator projection to Latitude Longitude
plot (mapLatLong)
points (UCplaces$long, UCplaces$lat, pch=16) #add the points
text (UCplaces$long, UCplaces$lat, labels = UCplaces$names, pos=3) #add labels
# b) change the background context map, [hint: ?openmap will give you a list of possible types]
map2 <- openmap(c(38.55, -121.77), c(38.53, -121.74), zoom = NULL, type = "stamen-watercolor")
plot(map2)
map2LatLong <- openproj(map2)
plot(map2LatLong)
points (UCplaces$long, UCplaces$lat, pch=16)
text (UCplaces$long, UCplaces$lat, labels = UCplaces$names, pos=3)
#c) change the extent of the map to include more of Davis, and change the size and appearance of the points and labels as appropriate
map3 <- openmap(c(38.562083, -121.787334), c(38.526302, -121.728406), zoom = NULL, type = "esri")
plot(map3)
map3LatLong <- openproj(map3)
plot(map3LatLong)
points (UCplaces$long, UCplaces$lat, col= "blue", bg="blue", cex= 1.5, pch=23)
text(UCplaces$long, UCplaces$lat, labels = UCplaces$names, col = "blue", cex = 1.2, pos=4)